package com.fragment;

import java.net.NetPermission;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;

import com.activities.Front_items_activity;
import com.bean.feeds;
import com.dbs.DataHelper;
import com.example.lizhiinformation.R;
import com.me.maxwin.view.XListView;
import com.me.maxwin.view.XListView.IXListViewListener;
import com.myadapter.FragmentAllAdapter;
import com.ownload.Httputil;
import com.ownload.Httputil.OnNetWorkRespones;
import com.tools.Json_All;

import android.R.integer;
import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ImageView;
import android.widget.ListView;

@SuppressLint("ValidFragment")
public class FragmentNoTop extends Fragment implements IXListViewListener {
	private String path1;
	private String path2;
	FragmentAllAdapter adapter;
	private int imagepath;

	@SuppressLint("ValidFragment")
	public FragmentNoTop(String path1, String path2, int imagepath) {
		super();
		this.path1 = path1;
		this.path2 = path2;
		this.imagepath = imagepath;

	}

	ListView listview;
	int code = 1;

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		return inflater.inflate(R.layout.fragment_notop, null);
	}

	@Override
	public void onViewCreated(View view, Bundle savedInstanceState) {
		// TODO Auto-generated method stub

		super.onViewCreated(view, savedInstanceState);
		listview = (ListView) view.findViewById(R.id.FragmentNoToplistview);
		listview.setVisibility(View.GONE);
		ImageView imageview = (ImageView) view
				.findViewById(R.id.FragmentNOTopimagview);
		imageview.setBackgroundResource(imagepath);
		((XListView) listview).setPullLoadEnable(true);
		((XListView) listview).setXListViewListener(this);
		setlistview();
		frame();
		

	}

	@Override
	public void onRefresh() {
		// TODO Auto-generated method stub
		setlistview();
		onLoad();
	}

	@Override
	public void onLoadMore() {
		// TODO Auto-generated method stub
		code++;
		if(new DataHelper(getActivity()).query(path1+String.valueOf(code)+path2, "没头部")==null){
			addlistview(listview, code);
		}else{
			ArrayList<feeds> list1 = new Json_All()
			.Json_All(new DataHelper(getActivity()).query(path1+String.valueOf(code)+path2, "没头部"));
	        adapter.addData(list1);
		}
		onLoad();

	}

	public void setlistview() {
		Httputil.OnWorkDown(path1 + "1" + path2, new OnNetWorkRespones() {

			@Override
			public void OK(String respose) {
				// TODO Auto-generated method stub
				new DataHelper(getActivity()).insert(respose, "没头部", path1 + 1
						+ path2);
				ArrayList<feeds> json_All = new Json_All().Json_All(new DataHelper(
						getActivity()).query(path1 + "1" + path2, "没头部"));
				adapter = new FragmentAllAdapter(getActivity(), json_All, listview);
				listview.setAdapter(adapter);
				listview.setVisibility(View.VISIBLE);
			}

			@Override
			public void ERROR(String error) {
				// TODO Auto-generated method stub

			}
		});
	}

	// listview加载更多数据
	public void addlistview(ListView listview, final int code) {
		Httputil.OnWorkDown(path1 + String.valueOf(code) + path2,
				new OnNetWorkRespones() {

					@Override
					public void OK(String respose) {
						// TODO Auto-generated method stub
						new DataHelper(getActivity()).insert(respose, "没头部", path1 + String.valueOf(code) + path2);
						
						ArrayList<feeds> list1 = new Json_All()
								.Json_All(respose);
						adapter.addData(list1);
					}

					@Override
					public void ERROR(String error) {
						// TODO Auto-generated method stub

					}
				});
	}

	// 停止加载和刷新动画
	private void onLoad() {
		((XListView) listview).stopRefresh();
		((XListView) listview).stopLoadMore();
		SimpleDateFormat sdf = new SimpleDateFormat("MM月dd日      HH:mm ");
		long millis = System.currentTimeMillis();
		Date date = new Date(millis);
		String format = sdf.format(date);
		((XListView) listview).setRefreshTime(format);

	}

	public void frame() {
		if (new DataHelper(getActivity()).query(path1 + "1" + path2, "没头部") != null) {
			ArrayList<feeds> json_All = new Json_All().Json_All(new DataHelper(
					getActivity()).query(path1 + "1" + path2, "没头部"));
			adapter = new FragmentAllAdapter(getActivity(), json_All, listview);
			listview.setAdapter(adapter);
			listview.setVisibility(View.VISIBLE);
		}else{
			setlistview();
		}
		if(new DataHelper(getActivity()).query(path1 + String.valueOf(code) + path2, "没头部")!=null){
			ArrayList<feeds> list1 = new Json_All()
			.Json_All(new DataHelper(getActivity()).query(path1 + String.valueOf(code) + path2,"没头部"));
	       adapter.addData(list1);
		}else{
			setlistview();
		}
		listview.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {
				// TODO Auto-generated method stub
				Intent intent = new Intent(getActivity(),
						Front_items_activity.class);
				intent.putExtra("url", adapter.getItem(arg2 + 1));
				startActivity(intent);
			}
		});
	}
}
